package com.nl.lib_core.tool.extends

import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.annotation.Px
import androidx.constraintlayout.helper.widget.Flow
import androidx.constraintlayout.utils.widget.ImageFilterView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.MarginLayoutParamsCompat

/** 作者: wangfei 创建时间: 2021/12/3 包名: com.nl.lib_core.util.extends 类名: layout */
inline fun ViewGroup.TextView(block: TextView.() -> Unit) =
    TextView(context).apply(block).also { addView(it) }

inline fun ViewGroup.ImageFilterView(block: ImageFilterView.() -> Unit) =
    ImageFilterView(context).apply(block).also { addView(it) }

inline fun ViewGroup.ImageView(block: ImageView.() -> Unit) =
    ImageView(context).apply(block).also { addView(it) }

inline fun ViewGroup.LinearLayout(init: LinearLayout.() -> Unit) =
    LinearLayout(context).apply(init).also { addView(it) }

inline fun ViewGroup.ConstraintLayout(init: ConstraintLayout.() -> Unit) =
    ConstraintLayout(context).apply(init).also { addView(it) }

inline fun View.Flow(init: Flow.() -> Unit) = Flow(context).apply(init)

inline fun matchConstraintLayoutParams(
    id: Int = 0,
    @Px leftMargin: Int = 0,
    @Px marginEnd: Int = 0
) =
    ConstraintLayout.LayoutParams(
            ConstraintLayout.LayoutParams.MATCH_PARENT, ConstraintLayout.LayoutParams.MATCH_PARENT)
        .apply {
          this.startToStart = id
          this.endToEnd = id
          this.topToTop = id
          this.bottomToBottom = id
          this.leftMargin = leftMargin
          this.marginEnd = marginEnd
        }

inline var View.margin_top: Float
  get() {
    return -1.9f
  }
  set(value) {
    (layoutParams as? ViewGroup.MarginLayoutParams)?.apply { topMargin = context.dp2px(value) }
  }

inline fun cLayoutParams(w: Int, h: Int) = ConstraintLayout.LayoutParams(w, h)

inline fun cWLayoutParams() =
    ConstraintLayout.LayoutParams(
        ConstraintLayout.LayoutParams.WRAP_CONTENT, ConstraintLayout.LayoutParams.WRAP_CONTENT)

inline var View.margin_bottom: Float
  get() {
    return -1.0f
  }
  set(value) {
    (layoutParams as? ViewGroup.MarginLayoutParams)?.apply { bottomMargin = context.dp2px(value) }
  }

inline var View.margin_start: Float
  get() {
    return -1.0f
  }
  set(value) {
    (layoutParams as? ViewGroup.MarginLayoutParams)?.apply {
      MarginLayoutParamsCompat.setMarginStart(this, context.dp2px(value))
    }
  }

inline var View.margin_end: Float
  get() {
    return -1.0f
  }
  set(value) {
    (layoutParams as? ViewGroup.MarginLayoutParams)?.apply {
      MarginLayoutParamsCompat.setMarginEnd(this, context.dp2px(value))
    }
  }

fun String.toLayoutId(): Int {
  var id = java.lang.String(this).bytes.sum()
  if (id == 48) id = 0
  return id
}

inline var View.layout_id: String
  get() {
    return ""
  }
  set(value) {
    id = value.toLayoutId()
  }
